import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import http from '@/utils/request.js'

const store = new Vuex.Store({
	state: {
	  loginStatus: false,
	  uid: '',
	  uploadUrl: '',
	  statusHeight: uni.getSystemInfoSync().statusBarHeight,
	  userInfo: {
	  }
	},
	mutations: {
	  // 修改uid
	  setUid (state, uid) {
		  state.uid = uid
		  uni.setStorageSync('uid', uid)
	  },
	  // 修改用户登录状态
	  setLoginStatus (state, status = false) {
		state.loginStatus = status
		uni.setStorageSync('loginStatus', status)
	  },
	  // 修改用户信息
	  setUserinfo (state, info) {
		  state.userInfo = info
		  uni.setStorageSync('userInfo', JSON.stringify(info))
	  },
	  // 修改上传图片路径
	  setUploadUrl (state, url) {
		  state.uploadUrl = url
	  }
	},
	actions: {
		// 异步获取用户信息
		async getUserInfo({commit, state}) {
			const res = await http.httpRequest('/user/index','post',{uid: state.uid})
			if (res.data.code == 1) {
				commit('setLoginStatus', true),
				commit('setUserinfo', res.data.data.member)
				commit('setUid', res.data.data.uid)
			} else {
				uni.showToast({
					icon: 'none',
					title: '获取用户信息失败'
				})
			}
		},
	}
})

export default store
